import positionlist from "../view/positionlist.html";
import positionItem from "../view/position.html";
import fetch from '../model/fetch';
import BScroll from 'better-scroll';


export default class PositionController {

    constructor() {
        this.pageNo = 1;
        this.pageSize = 15;
        this.datalist = [];

    }
    async getlist() {
        let list = await new fetch().getPositionlist({
            pageNo: this.pageNo,
            pageSize: this.pageSize
        });
        let result = list.content.data.page.result;
        this.datalist = this.datalist.concat(result);
        return this.datalist;
    }
    async render() {
        var self = this;
        $(".swiper-slide").eq(0).html(positionlist);//把position结构添加到主页面
        $(".loading").attr("style", "display:flex");
        var htmlStr = template.render(positionItem, { data: await self.getlist() });
        $(".positionlist").html(htmlStr);//渲染position数据
        $(".loading").attr("style", "display:none");
        let scroll = new BScroll("#main-position", {
            probeType: 2,
            click: true,
            scrollY: true
        })
        scroll.scrollTo(0, -35);

        scroll.on('scroll', function () {
            console.log(this.y)
            if (this.y >= 0) {
                $(".reload img").attr('src', "./images/refresh.gif");
            }
        });
        scroll.on('touchEnd', async function () {
            // 刷新页面
            if (this.y > 0) {
                var htmlStr = template.render(positionItem, { data: await self.getlist() });
                $(".positionlist").html(htmlStr);
                $(".reload img").attr('src', "./images/arrow.png")
                scroll.scrollTo(0, -35);
            }
            //翻页
            if (this.y < this.maxScrollY) {
                console.log(this.y)
                self.pageNo++;
                var htmlStr = template.render(positionItem, { data: await self.getlist() });
                $(".positionlist").html(htmlStr);
                scroll.refresh();
                scroll.scrollTo(0, -35);
            }
        })

        this.initEvent();
    }

    initEvent() {
        $(".positionlist").on('click', "li", function () {
            let positionId = $(this).attr("data-id");
            location.hash = "details/" + positionId;
        })
    }
}